home *** CD-ROM | disk | FTP | other *** search
- Path: Rezonet.net!news
- From: ray@ultimate-tech.com (Ray Dunn)
- Newsgroups: comp.lang.c
- Subject: Re: Floating point calculation order
- Date: 25 Jan 1996 17:09:21 GMT
- Organization: Ultimate Technographics Inc.
- Message-ID: <4e8dg1$ehg@ns.RezoNet.NET>
- References: <m0tedv8-0002eqC@sice.nsk.su> <3104c6d9.134061184@nntp.ix.netcom.com> <DLnE5K.2xH@microunity.com> <822428038snz@genesis.demon.co.uk>
- NNTP-Posting-Host: 204.19.230.7
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In referenced article, Lawrence Kirby says...
- >Parentheses in C allow you to change the operator/operand grouping
- >within expressions. They don't enforce any order of evaluation beyond
- >that implied by the grouping. So:
- >
- > x = p * q / r;
- >
- >and
- >
- > x = (p * q) / r;
- >
- >mean *exactly* the same thing in C since the parentheses don't change
- >the grouping.
-
- You're right, of course, but they only mean the same thing because *
- and / have the same precedence and associate left to right.
-
- Remember that the original posting was from someone complaining that
- the division was done first (presumably on a non-conforming compiler).
- In this case parenthesizing the multiplication would indeed change the
- order of evaluation and solve his problem.
-
- I do think it's a good idea to parenthesize expressions to make it
- clear what you expect to happen.
-
- It's probably helpful to use this opportunity to remind beginners that
- although the * is guaranteed to be done before the /, there is no
- definition of which order the *operands* will be evaluated, so for
- example, in:
-
- x = f() * g() / h();
-
- the calls of f, g, and h may be made in any order the compiler chooses,
- even if terms are parenthesised and/or operators of different
- precedence are used.
- --
- Ray Dunn (opinions are my own) | Phone: (514) 938 9050
- Montreal | Phax : (514) 938 5225
- ray@ultimate-tech.com | Home : (514) 630 3749
-
-